package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import services.ServiceCart;

public class ServletRemoveProductFromCart extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        ServiceCart serviceCart = new ServiceCart();

        HttpSession session = request.getSession();
        String userIdsession = (String) session.getAttribute("userId");
        int userId = Integer.parseInt(userIdsession);

        String pId = request.getParameter("pId");
        int productId = Integer.parseInt(pId);

        String output = serviceCart.removeProductCart(userId, productId);

        out.print("<center><h5>Product " + output + "</h5></center>");
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServiceCart serviceCart = new ServiceCart();
        int userId = Integer.parseInt(request.getParameter("androidUserId"));
        System.out.println("androidUserId========== " + userId);
        int pId = Integer.parseInt(request.getParameter("androidProductId"));
        System.out.println("androidProductId========== " + pId);
        String output = serviceCart.removeProductCart(userId, pId);
        response.getWriter().print(output);
    }
}
